<?php 
namespace Admin\Model;
use Think\Model;

class AdvsModel extends Model
{
	//执行插叙方法
	public function advs_sec(){
		$list = $this->select();
		//数据处理
		$status = ['1'=>'显示','2'=>'隐藏'];
		foreach ($list as $key => &$value) {
			$value['status'] = $status[$value['status']];
			$value['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
		}
		return $list;
	}
	//执行添加操作
	public function add_advs(){
		$info = I('post.');
		if(empty($info['advsname']) || empty($info['advslink'])){
			return false;
		}
		// return $_FILES;
        //存在上传图片就处理
        if (!empty($_FILES['picname']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  =      '/comm'; // 设置附件上传目录 
            $upload->subName   =    array('date','Ymd');  
             // 上传文件    
            $pic   =   $upload->upload();
            if(!$pic) {
               // 上传错误提示错误信息        
                $this->error($upload->getError());   
            }else{
                // 上传成功      
                $info['picname'] = $pic['picname']['savepath'].$pic['picname']['savename'];
            }
          
        }
        $info['addtime'] = time(); 
        //添加操作
        $res = $this->add($info);
        return $res;
	}
	//查询一条数据
	public function find_advs(){
		$id = I('get.id');
		//设置查询条件
		$map['id'] = ['eq',$id];
		//执行查询并接收返回值
		$list = $this->where($map)->find();
		return $list;
	}
	//执行修改操作
	public function advs_edit(){
		$info = I("post.");
        $opic = $info['opic'];
        unset($info['opic']);
        if(empty($info['advsname']) || empty($info['advslink'])){
			return false;
		}
        //处理图片
        if (!empty($_FILES['picname']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  =      '/comm'; // 设置附件上传目录 
            $upload->subName   =    array('date','Ymd');  
             // 上传文件    
            $pic   =   $upload->upload();
            if(!$pic) {
               // 上传错误提示错误信息        
                $this->error($upload->getError());   
            }else{
                // 上传成功      
                $info['picname'] = $pic['picname']['savepath'].$pic['picname']['savename'];
                @unlink("/TP/Uploads".$opic);
            }
        } else{
            $info['picname'] = $opic;
        }

        //更新数据
        $res = $this->save($info);
        return $res;
	}
	//执行删除操作
	public function del_advs(){
		$id = I('get.id');
		$res = $this->delete($id);
		return $res;
	}
}